<?php
require_once("Image/Transform.php");
require_once("PEAR.php");
require_once("ArtistWiki.php");
require_once("Info.php");
require_once("ArtistMatchItem.php");
require_once("LineupItem.php");

/**
 * Class for parsing data from the Last.fm webservices at http://www.audioscrobbler.net/data/webservices/ 
 * and caches it.
 * 
 * @author Magnus Stoveland <mstoveland@gmail.com>
 * 
 */
class Lastfm 
{

    /**
     * Matches array of artists and array of top artists from a tag.
     *
     * @param array $program_artists
     * @param array $profile_artists
     * @return associative array with degree of match [$program_id] => [$score]
     */
    function match($lineup, $inputArtists, $input) {
        //convert $userArtists to "lookup-array"luft
        foreach($inputArtists as $artistMatchItem) {
            foreach($artistMatchItem->getSimilarArtits() as $artist) {
                $lookupUserArtists[mb_strtolower($artist->name, "UTF-8")] = $artist->weight;
            }
        }
        $i = 0; 
        $score = 0;
        foreach ($lineup as $id => &$lineupItem) { //$lineup is an array of Artist-objects
            //Info::dump("artist name: ".$lineupItem->artist->name);
            $similarArtists = $lineupItem->artist->getSimilarArtits();
            //Info::dump($similarArtists);
            foreach ($similarArtists as $similar) {
                $artistMatchName = mb_strtolower($similar->name, "UTF-8");
                // Check for match 
                if (array_key_exists($artistMatchName, $lookupUserArtists)) {
                    $match = new ArtistMatchItem();
                    $match->artist = $similar;
                    $match->matchPercentage = $lookupUserArtists[$artistMatchName];
                    //echo $lineupItem->artist->name." MATACHING ".$artistMatchName." - ".$match->matchPercentage."<br>";
                    $lineupItem->artist->matchingArtists[] = $match;
                    $score += $match->matchPercentage;
                }
            }
            $ranking[$id] = $score;
            // For the next iteration, to keep the index in the program of artist with no matches with proifile
            $score = $i; 
            $i -= 0.000001;   
        }
        asort($ranking);
        return Lastfm::_merge($lineup, $ranking);
    }
    
    /**
     * Used in association with Lastfm::match() to merge match score and program.
     * @see match() 
     * 
     * @param array $program_artists
     * @param array $ranking
     * @return array with sorted program
     */
    function _merge($lineup, $ranking) {
        $keys = array_keys($ranking);
        for( $i = count($lineup)-1; $i >= 0; $i-- ) {
           $resultLineup[$keys[ $i ]] = $lineup[ $keys[ $i ] ];
        }
        
        foreach($resultLineup as &$lineupItem) {
            foreach($lineupItem->artist->matchingArtists as $matchingArtist) {
                $lineupItem->artist->weight += $matchingArtist->matchPercentage;
            };
            if($lineupItem->artist->weight >= 1) {
                $lineupItem->artist->weight = round($lineupItem->artist->weight);
                $output[] = $lineupItem;
            }
        }
        usort($resultLineup, "resultSort");
        return $output;
    }
}
?>